const Koa = require('koa');
const fs = require('fs-extra');
const path = require('path');
const router = require('koa-router')();
const static = require('koa-static');
const koaBody = require('koa-body');

const axios = require('./routes/index.js');
const upload = require('./routes/upload.js');
const merge = require('./routes/merge.js');
const verify = require('./routes/verify.js');


const app = new Koa();

app.use(static(path.join(__dirname, "./static")));
app.use(static(path.join(__dirname, "./target")));
app.use(static(path.join(__dirname, "./public")));

app.use(koaBody({
  jsonLimit: '50mb',
  formLimit: '50mb',
  textLimit: '50mb',
  multipart: true, // 支持文件上传
  // encoding: 'gzip', // 启用这个会报错
  formidable: {
    // uploadDir: path.join(__dirname, './public'), // 设置文件上传目录
    keepExtensions: true,    // 保持文件的后缀
    maxFieldsSize: 1024 * 1024 * 1024, // 文件上传大小
    // onFileBegin: (name, file) => { } // 文件上传前的设置
  }
}));

router.post('/file', upload);
router.post('/merge', merge);
router.get('/axios', axios);
router.post('/verify', verify);

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000, () => {
  console.log("Server running on port 3000")
});